﻿using System;
using Microsoft.Win32;
using System.Diagnostics;

namespace Ahmad.A.Ousman.WindowsProxy.Common
{
    public class Service
    {
        #region Methods
        public static void SetProxy(Proxy proxy)
        {
            RegistryKey registry = Registry.CurrentUser.OpenSubKey(Proxy.INTERNET_SETTINGS, true);
            if (registry != null)
            {
                if (proxy != null && !String.IsNullOrEmpty(proxy.DnsDomain))
                {
                    registry.SetValue(Proxy.PROXY_ENABLED, (proxy.EnableProxyServer) ? 1 : 0);
                    registry.SetValue(Proxy.PROXY_SERVER, proxy.ProxyServer);
                    registry.SetValue(Proxy.PROXY_EXCEPTIONS, proxy.ProxyExceptions);
                }
                else
                {
                    registry.SetValue(Proxy.PROXY_ENABLED, 0);
                    registry.SetValue(Proxy.PROXY_SERVER, String.Empty);
                    registry.SetValue(Proxy.PROXY_EXCEPTIONS, String.Empty);
                }
            }
        }
        #endregion
    }

    public class Utility
    {
        public static void Log(String source, String message, EventLogEntryType type)
        {
            const string S_LOG = "Application";

            if (!EventLog.SourceExists(source))
            {
                EventLog.CreateEventSource(source, S_LOG);
            }

            EventLog.WriteEntry(source, message, type);
        }
    }
}
